package com.qilin.hammer;

import static com.qilin.hammer.util.Global.ORCLE_DATABASE;

import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

import com.qilin.hammer.util.database.DBConnection;
import com.qilin.hammer.util.database.DBFactory;
import com.qilin.hammer.util.database.DBOperator;

public class EnrichTestHelper {
	
	private DBOperator orcloperator;
	private Random r;
	
	{
		r = new Random(47);
		orcloperator = null;
	}
	
	public void createTable(String tname, Map<String, String> fields){
		String sql = "create table "+tname+" (";
		for(Map.Entry<String, String> field : fields.entrySet())
			sql+=field.getKey()+" "+field.getValue()+",";
		sql = sql.substring(0, sql.length()-1);
		sql+=")";
		System.out.println(sql);
		this.orcloperator.createTable(sql);
		
	}
//	
//	private <T> T getValue(T a, T b, Class<?> type,int count, int step){
//		Constructor c = type.getConstructors();
//		return count*(b-a)/step);
//	}
	
	public void CreateEnrichmentTestData(int size, boolean isPurge){
		DBFactory dbfactory = new DBFactory();
    	DBConnection orclconnector=dbfactory.getDBConnectionInstance(ORCLE_DATABASE);
    	Connection orclconn = orclconnector.getConnection("10.111.3.50", "1521", "ZHANGRM", "123456");
    	orcloperator=new DBOperator(orclconn);
    	
    	String tname = "enrichment_test_data";
    	Map<String, String> fields = new LinkedHashMap<String, String>();
    	
    	fields.put("ID", "NUMBER");
    	fields.put("BOOL_DATA", "VARCHAR2(10)");
    	fields.put("BYTE_DATA", "NUMBER");
    	fields.put("SHORT_DATA", "NUMBER");
    	fields.put("INT_DATA", "NUMBER");
    	fields.put("LONG_DATA", "NUMBER");
    	fields.put("DOUBLE_DATA", "NUMBER");
    	fields.put("FLOAT_DATA", "NUMBER");
    	fields.put("TIME_DATA", "TIMESTAMP(6)");
    	fields.put("DATE_DATA", "DATE");
    	fields.put("DATETIME_DATA", "TIMESTAMP(6)");
    	fields.put("STR_DATA", "VARCHAR2(32)");

		byte bytes[]={Byte.MAX_VALUE, Byte.MIN_VALUE, -100, 100};
		short shorts[]={Short.MAX_VALUE, Short.MIN_VALUE, -10000, 10000};
		int ints[]={Integer.MAX_VALUE, Integer.MIN_VALUE, -100000000, 100000000};
		long longs[]={Long.MAX_VALUE, Long.MIN_VALUE, -1000000000, 1000000000};
		float floats[]={Float.MAX_VALUE, Float.MIN_VALUE, -10000.0F, 10000.0F};
		double doubles[]={1.11111111111111111D, -1.11111111111111111D, -100000000.0, 100000000.0};
			
		if(orcloperator.isExist(tname)){
    		if(isPurge) orcloperator.execute("delete from "+tname);
		}else{
    		this.createTable(tname, fields);
		}
		String str="hello world";
		String types[] = {"first", "second", "third"};
		int steps = size;
		
    	for(int i=0;i<=steps+2;i++){
    		String sql="insert into "+tname+" values ";
    		str = types[i%types.length];
    		if(i==0||i==1){
    			sql +="("+(i+1)+","+(i==0?"'true'":"'false'")+","+bytes[i]+","+shorts[i]+","+ints[i]+","
    				+longs[i]+","+doubles[i]+","+floats[i]+",sysdate,sysdate,sysdate,"+"'db"+str+"')";
    		}else{
    			sql +="("+(i+1)+","+(i%2==0?"'true'":"'false'")+","
    			+(bytes[2]+(i-2)*(bytes[3]-bytes[2])/steps)+","
    			+(shorts[2]+(i-2)*(shorts[3]-shorts[2])/steps)+","
    			+(ints[2]+(i-2)*((ints[3]-ints[2])/steps))+","
				+(longs[2]+(i-2)*(longs[3]-longs[2])/steps)+","
				+(doubles[2]+(i-2)*(doubles[3]-doubles[2])/steps)+","
				+(floats[2]+(i-2)*(floats[3]-floats[2])/steps)+",sysdate,sysdate,sysdate,"+"'db"+str+"')";
    		}
    		System.out.println(sql);
    		orcloperator.execute(sql);
    		//insert into multitypedata values (1,'Correct',1,127,32767,2147483647,9223372036854775807,0,3.4028235E38,to_date('2011-11-12 12:12:12','yyyy-MM-dd hh24:mi:ss'),to_date('21-08-2003','DD-MM-YYYY'),to_date('21-08-2003','DD-MM-YYYY'),'hello world','hello world')
    	}
    	this.orcloperator.commit();
	} 
	
	public static void main(String args[]){
		new EnrichTestHelper().CreateEnrichmentTestData(100, true);
	}
}
